package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.YouxuanStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YouxuanGoodsMapper;
import com.ruoyi.system.domain.YouxuanGoods;
import com.ruoyi.system.service.IYouxuanGoodsService;

/**
 * 商品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-12-27
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class YouxuanGoodsServiceImpl implements IYouxuanGoodsService 
{
    private BigDecimal num = new BigDecimal("100");
    @Autowired
    private YouxuanGoodsMapper youxuanGoodsMapper;

    @Override
    public String selectProductDetailById(Long id) {
        return youxuanGoodsMapper.selectProductDetailById(id);
    }

    /**
     * 查询商品管理
     * 
     * @param id 商品管理ID
     * @return 商品管理
     */
    @Override
    public YouxuanGoods selectYouxuanGoodsById(Long id)
    {
        YouxuanGoods youxuanGoods = youxuanGoodsMapper.selectYouxuanGoodsById(id);
        if (youxuanGoods.getBasePrice()!=null)
            youxuanGoods.setBasePrice(youxuanGoods.getBasePrice().divide(num));
        if (youxuanGoods.getBidPrice()!=null)
            youxuanGoods.setBidPrice(youxuanGoods.getBidPrice().divide(num));
        if(youxuanGoods.getProductImgString() != null && !youxuanGoods.getProductImgString().equals(""))
            youxuanGoods.setProductImg(youxuanGoods.getProductImgString().split(","));
        return youxuanGoods;
    }

    /**
     * 查询商品管理列表
     * 
     * @param youxuanGoods 商品管理
     * @return 商品管理
     */
    @Override
    public List<YouxuanGoods> selectYouxuanGoodsList(YouxuanGoods youxuanGoods)
    {

        List<YouxuanGoods> list =youxuanGoodsMapper.selectYouxuanGoodsList(youxuanGoods);
        for (YouxuanGoods y: list
             ) {
            if (y.getBasePrice()!=null)
                y.setBasePrice(y.getBasePrice().divide(num));
            if (y.getBidPrice()!=null)
                y.setBidPrice(y.getBidPrice().divide(num));
            if(y.getProductImgString() != null && !y.getProductImgString().equals(""))
                y.setProductImg(y.getProductImgString().split(","));
        }
        return list;
    }

    /**
     * 新增商品管理
     * 
     * @param youxuanGoods 商品管理
     * @return 结果
     */
    @Override
    public int insertYouxuanGoods(YouxuanGoods youxuanGoods)
    {

        StringBuffer sb = new StringBuffer();
        if (youxuanGoods.getBasePrice()!=null)
            youxuanGoods.setBasePrice(youxuanGoods.getBasePrice().multiply(num));
        if (youxuanGoods.getBidPrice()!=null)
            youxuanGoods.setBidPrice(youxuanGoods.getBidPrice().multiply(num));
        if (youxuanGoods.getProductImg().length != 0) {
            for (int i=0;i<youxuanGoods.getProductImg().length;i++
            ) {
                if (i!=youxuanGoods.getProductImg().length-1)
                    sb.append(youxuanGoods.getProductImg()[i]+",");
                else
                    sb.append(youxuanGoods.getProductImg()[i]);
            }
            youxuanGoods.setProductImgString(sb.toString());
        }
        YouxuanStock youxuanStock = new YouxuanStock();

        youxuanStock.setStock(youxuanGoods.getStock());
        youxuanStock.setSecKillTotal(youxuanGoods.getSecKillTotal());
        youxuanStock.setSecKillStock(youxuanGoods.getSecKillStock());
        youxuanGoodsMapper.insertYouxuanGoods(youxuanGoods);
        youxuanStock.setGoodsId(youxuanGoods.getId());
        return youxuanGoodsMapper.insertYouxuanStock(youxuanStock);
    }

    /**
     * 修改商品管理
     * 
     * @param youxuanGoods 商品管理
     * @return 结果
     */
    @Override
    public int updateYouxuanGoods(YouxuanGoods youxuanGoods)
    {
        StringBuffer sb = new StringBuffer();
        if (youxuanGoods.getBasePrice()!=null)
            youxuanGoods.setBasePrice(youxuanGoods.getBasePrice().multiply(num));
        if (youxuanGoods.getBidPrice()!=null)
            youxuanGoods.setBidPrice(youxuanGoods.getBidPrice().multiply(num));
        if (youxuanGoods.getProductImg().length != 0) {
            for (int i=0;i<youxuanGoods.getProductImg().length;i++
            ) {
                if (i!=youxuanGoods.getProductImg().length-1)
                    sb.append(youxuanGoods.getProductImg()[i]+",");
                else
                    sb.append(youxuanGoods.getProductImg()[i]);
            }
            youxuanGoods.setProductImgString(sb.toString());
        }
        youxuanGoodsMapper.deleteYouxuanStock(youxuanGoods.getId());
        YouxuanStock youxuanStock = new YouxuanStock();
        youxuanStock.setGoodsId(youxuanGoods.getId());
        youxuanStock.setStock(youxuanGoods.getStock());
        youxuanStock.setSecKillTotal(youxuanGoods.getSecKillTotal());
        youxuanStock.setSecKillStock(youxuanGoods.getSecKillStock());
        youxuanGoodsMapper.insertYouxuanStock(youxuanStock);
        youxuanGoods.setProductImgString(sb.toString());
        return youxuanGoodsMapper.updateYouxuanGoods(youxuanGoods);
    }

    /**
     * 批量删除商品管理
     * 
     * @param ids 需要删除的商品管理ID
     * @return 结果
     */
    @Override
    public int deleteYouxuanGoodsByIds(Long[] ids)
    {
        for (int i=0;i<ids.length;i++){
            youxuanGoodsMapper.deleteYouxuanStock(ids[i]);
        }
        return youxuanGoodsMapper.deleteYouxuanGoodsByIds(ids);
    }

    /**
     * 删除商品管理信息
     * 
     * @param id 商品管理ID
     * @return 结果
     */
    @Override
    public int deleteYouxuanGoodsById(Long id)
    {

        youxuanGoodsMapper.deleteYouxuanStock(id);
        return youxuanGoodsMapper.deleteYouxuanGoodsById(id);
    }
}
